package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.DishFlavor;
import com.sky.mapper.DishMapper;
import com.sky.entity.Dish;
import com.sky.service.DishFlavorService;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-07-06
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    DishMapper dishMapper;

    @Resource
    DishFlavorService dishFlavorService;
    @Override
    public Page<DishVO> pageDish(Page<DishVO> dishPage,DishPageQueryDTO dishPageQueryDTO) {
        Page<DishVO> dishVOPage = dishMapper.pageDish(dishPage,dishPageQueryDTO);
        List<DishVO> records = dishVOPage.getRecords();
        records.stream().map((dish) -> {
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dish.setFlavors(dishFlavorList);
            return dish;
        }).collect(Collectors.toList());
        dishVOPage.setRecords(records);
        return dishVOPage;
    }

    @Override
    public List<Dish> setmealQueryDish(Long id) {

        return baseMapper.setmealQueryDish(id);
    }

    @Override
    public List<DishItemVO> setmealQueryDishNO2(Long id) {
        return baseMapper.setmealQueryDishNO2(id);
    }
}
